home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / MTRECAL.PAK / SLOWCALC.CPP < prev    next >
C/C++ Source or Header  |  1997-05-06  |  3KB  |  100 lines

  1. // slowcalc.cpp : implementation of the recalc engine, which is reused by
  2. //                both demonstrations of recalculating: single thread,
  3. //                and worker thread.
  4. //
  5. // This is a part of the Microsoft Foundation Classes C++ library.
  6. // Copyright (C) 1992-1995 Microsoft Corporation
  7. // All rights reserved.
  8. //
  9. // This source code is only intended as a supplement to the
  10. // Microsoft Foundation Classes Reference and related
  11. // electronic documentation provided with the library.
  12. // See these sources for detailed information regarding the
  13. // Microsoft Foundation Classes product.
  14.  
  15. #include "stdafx.h"
  16. #include "calcthrd.h"
  17. #include "recaldoc.h"
  18. #include "slowcalc.h"
  19. #include "mtrecalc.h"
  20.  
  21.  
  22. BOOL SlowAdd(int nInt1, int nInt2, int& nResult, CRecalcThreadInfo* pInfo, int nSeconds,
  23.     HWND hwndNotifyProgress)
  24. {
  25.     CWnd* pWndNotifyProgress = CWnd::FromHandle(hwndNotifyProgress);
  26.  
  27.     BOOL bRestartCalculation = TRUE;
  28.     while (bRestartCalculation)
  29.     {
  30.         bRestartCalculation = FALSE;
  31.         // Do the calculation 5% at a time, allowing for interruption to
  32.         // restart calculation.
  33.         for (int nCount = 1; nCount < 20; nCount++)
  34.         {
  35.             if (pInfo != NULL
  36.                 && WaitForSingleObject(pInfo->m_hEventKillRecalcThread, 0)  == WAIT_OBJECT_0)
  37.             {
  38.                 if (hwndNotifyProgress != NULL)
  39.                 {
  40.                     // Reset progress indication to 0 (recalculation not begun)
  41.                     pWndNotifyProgress->PostMessage(WM_USER_RECALC_IN_PROGRESS);
  42.                 }
  43.                 return FALSE; // Terminate this recalculation
  44.             }
  45.  
  46.             if (pInfo != NULL
  47.                 &&WaitForSingleObject(pInfo->m_hEventStartRecalc, 0) == WAIT_OBJECT_0)
  48.             {
  49.                 // Get new source data for the calculation.
  50.                 nInt1 = pInfo->m_nInt1;
  51.                 nInt2 = pInfo->m_nInt2;
  52.                 bRestartCalculation = TRUE;
  53.                 continue;
  54.             }
  55.  
  56.             // Notify window about each 5% of progress
  57.             if (hwndNotifyProgress != NULL)
  58.             {
  59.                 // If calculating in a single thread, send rather than post the
  60.                 // "in progress" message, so that the progress is reflected
  61.                 // immediately in the status bar.  Otherwise, the posted "in
  62.                 // progress" messages won't be handled until after the recalculation
  63.                 // has totally completed.
  64.                 //
  65.                 // If calculating in a separate thread, however, then post rather
  66.                 // than send the "in progress" message.  Otherwise, in the case where
  67.                 // the main thread tries to kill the worker thread, there may be
  68.                 // a deadlock where the main thread is doing a WaitForSingleObject
  69.                 // on the "kill done" event while the worker thread is waiting for
  70.                 // the main thread to handle the sent message.
  71.                 if (pInfo == NULL)
  72.                 {
  73.                     pWndNotifyProgress->SendMessage(WM_USER_RECALC_IN_PROGRESS, nCount*5);
  74.                 }
  75.                 else
  76.                 {
  77.                     pWndNotifyProgress->PostMessage(WM_USER_RECALC_IN_PROGRESS, nCount*5);
  78.                 }
  79.             }
  80.             // Sleep 1/20th of total recalc time, which is 50 milliseconds
  81.             // per each of total number of seconds (nSeconds) of recalc time.
  82.             Sleep(nSeconds * 50);
  83.  
  84.         }
  85.  
  86.         if (hwndNotifyProgress != NULL)
  87.         {
  88.             // See comment above regarding post versus send message.
  89.             if (pInfo == NULL)
  90.                 pWndNotifyProgress->SendMessage(WM_USER_RECALC_IN_PROGRESS, 100);
  91.             else
  92.                 pWndNotifyProgress->PostMessage(WM_USER_RECALC_IN_PROGRESS, 100);
  93.  
  94.         }
  95.     }
  96.  
  97.     nResult = nInt1 + nInt2;
  98.     return TRUE;
  99. }
  100.